clear;
clc;
close all;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates figure 11 in the paper 
%------------------------------------------------------------------------%

data                                                                            = load('../01_Data/es_trades_5minute_EST.mat');

No_trades                                                                       = data.trades_per_min_mat;
No_contracts                                                                    = data.trade_quantity_per_min_mat;

% choose sample period ---------------------------------------------------------------------------------- %

start_date                  = datenum(2007,1,5);
finish_date                 = datenum(2020,12,29);

beg                         = find_closest(data.caldt_daily,start_date);
fin                         = find_closest(data.caldt_daily,finish_date);

caldt                       = data.caldt_daily(beg:fin);
No_trades                   = No_trades(beg:fin,:); 
Volume                      = No_contracts(beg:fin,:); 

fprintf(1,'********************************\n')
fprintf(1,'Sample: Emini          \n')
fprintf('begin %12s  \n',datestr(caldt(1)))
fprintf('finish %12s  \n',datestr(caldt(end)))
fprintf(1,'********************************\n')


%% ----------------------------------------------------------------% 
% ACCOUNT FOR DAYLIGHT SAVINGS
% ----------------------------------------------------------------% 
y_st = 1994;
%y_st = 1996;
y_fn = 2022;

local_timezone = {'Australia/Sydney' ; 'Pacific/Auckland' ; 'Asia/Tokyo' ; 'Europe/Zurich' ; 'America/New_York'; 'Europe/London'};

for tz = 1 : length(local_timezone)
    % Build long datetime vector with all intervals in the sample: HAVE TO DO THIS BASED ON THE UST datetime initial dates.
    t1 = datetime(y_st,1,1,18,0,0,'TimeZone', local_timezone{tz} );
    t2 = datetime(y_fn,1,1,18,0,0,'TimeZone', local_timezone{tz} );
    caldt_vec_local = (t1 : hours(24) : t2)';
    % compute UTC offset at beginning of day
    UTC_offset(:,tz) = tzoffset(caldt_vec_local(:,1));    
end

% daily vector NY TIME
t1                  = datetime(y_st,1,1,18,0,0,'TimeZone', 'UTC' );
t2                  = datetime(y_fn,1,1,18,0,0,'TimeZone', 'UTC' );
caldt_daily_ny      = (t1 : hours(24) : t2)';

% UTC offsets
UTC_offset          = hour(UTC_offset);
UTC_offset_AUD      = unique(UTC_offset(:,1))
UTC_offset_NZD      = unique(UTC_offset(:,2))
UTC_offset_JPY      = unique(UTC_offset(:,3))
UTC_offset_EUR      = unique(UTC_offset(:,4))
UTC_offset_EST      = unique(UTC_offset(:,5))
UTC_offset_GBP      = unique(UTC_offset(:,6))

% now compute date vectors split by daylight savings or non daylight savings times
caldt_DS_EST        = caldt_daily_ny(UTC_offset(:,5) == -4);
caldt_NDS_EST       = caldt_daily_ny(UTC_offset(:,5) == -5);

caldt_DS_EST_TEMP   = floor(datenum(caldt_DS_EST));
caldt_NDS_EST_TEMP  = floor(datenum(caldt_NDS_EST));


%intersection with data:
[C,IA,IB_DS]        = intersect(caldt_DS_EST_TEMP,caldt);
[C,IA,IB_NDS]       = intersect(caldt_NDS_EST_TEMP,caldt);

caldt_daily_DS      = caldt(IB_DS);
caldt_daily_NDS     = caldt(IB_NDS);

volume_min_mat_DS   = Volume(IB_DS,:);
volume_min_mat_NDS  = Volume(IB_NDS,:);


volume_min_mat_DS_rearrange                                                     = Volume(IB_DS,13:96);
volume_min_mat_NDS_rearrange                                                    = Volume(IB_NDS,1:84);
volume_per_min_mat_TOKYO_temp                                                   = volume_min_mat_DS_rearrange;
volume_per_min_mat_TOKYO_temp(end+1:end+length(volume_min_mat_NDS_rearrange),:) = volume_min_mat_NDS_rearrange;

volume_per_min_mat_TOKYO_temp(1:length(volume_min_mat_DS_rearrange),size(volume_min_mat_DS_rearrange,2)+1) = caldt_daily_DS;
volume_per_min_mat_TOKYO_temp(length(volume_min_mat_DS_rearrange)+1:length(volume_min_mat_DS_rearrange)+length(volume_min_mat_NDS_rearrange),size(volume_min_mat_NDS_rearrange,2)+1) = caldt_daily_NDS;


volume_per_min_mat_TOKYO = nan(size(volume_per_min_mat_TOKYO_temp,1),size(volume_per_min_mat_TOKYO_temp,2)-1);
indx = nan(size(caldt));
for i = 1:length(caldt)
    indx(i) = find(volume_per_min_mat_TOKYO_temp(:,end) == caldt(i));
    volume_per_min_mat_TOKYO(i,:) = volume_per_min_mat_TOKYO_temp(indx(i),1:end-1);
end


%% ----------------------------------------------------------------% 
% ----------------------------------------------------------------% 

volume_min_mat_DS_asia          = volume_min_mat_DS(:,1:96);
volume_min_mat_NDS_asia         = volume_min_mat_NDS(:,1:96);


%- VOLUME -%

%%%%% OVERNIGHT %%%%%

Volume_per_day_DS               = sum(volume_min_mat_DS_asia,2);
Volume_DS_normalized            = zeros(size(Volume_per_day_DS,1),size(volume_min_mat_DS_asia,2));

for i = 1 : length(Volume_per_day_DS)
    Volume_DS_normalized(i,:)    = volume_min_mat_DS_asia(i,:) ./ (1/(size(volume_min_mat_DS_asia,2)) * max(Volume_per_day_DS(i),1));
end

Volume_per_day_NDS              = sum(volume_min_mat_NDS_asia,2);
Volume_NDS_normalized           = zeros(size(Volume_per_day_NDS,1),size(volume_min_mat_NDS_asia,2));

for i = 1 : length(Volume_per_day_NDS)
    Volume_NDS_normalized(i,:)  = volume_min_mat_NDS_asia(i,:) ./ (1/(size(volume_min_mat_NDS_asia,2)) * max(Volume_per_day_NDS(i),1));
end

TTT_DS                          = length(Volume_DS_normalized);
mu_Volume_DS_normalized         = mean(Volume_DS_normalized,1);
var_Volume_DS_normalized        = var(Volume_DS_normalized,1,1);
se_Volume_DS_normalized         = sqrt(var_Volume_DS_normalized/TTT_DS);

TTT_NDS                         = length(Volume_NDS_normalized);
mu_Volume_NDS_normalized        = mean(Volume_NDS_normalized,1);
var_Volume_NDS_normalized       = var(Volume_NDS_normalized,1,1);
se_Volume_NDS_normalized        = sqrt(var_Volume_NDS_normalized/TTT_NDS);


hour_index                      = 0 : 1 : 96;
x_ticks                         = 0 : 12 : 96;
hour_string                     = {'18' , '19' , '20' , '21' , '22' , '23' , '24' , '01' , '02'};

mu_Volume_DS_normalized_plot    = [nan mu_Volume_DS_normalized];
se_Volume_DS_normalized_plot    = [nan se_Volume_DS_normalized];

mu_Volume_NDS_normalized_plot   = [nan mu_Volume_NDS_normalized];
se_Volume_NDS_normalized_plot   = [nan se_Volume_NDS_normalized];


% THE SPIKES AT 22:30 AND 23:30 ARE WHEN TOKYO RE-OPENS AFTER ITS LUNCH BREAK
% IT IS ONLY LATER IN THE SAMPLE PERIOD THAT WE SEE THE SPIKE AT TOKYO OPEN - IT IS NOT THERE IN THE EARLY YEARS
figure;
plot(hour_index, mu_Volume_DS_normalized_plot,'Linewidth',3);
hold on
plot(hour_index, mu_Volume_NDS_normalized_plot,'Linewidth',3);
xlim([0 96])
ylabel('Normalized Average Volume Traded' )
xlabel('Trade Time (ET)' )
legend('Daylight Savings Days','Non-Daylight Savings Days')
set(gca,'XTick',x_ticks,'XTickLabel',hour_string);
set(gca,'YAxisLocation','right');


